package org.spring.springboot.app.set.agenttype.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.spring.springboot.app.set.agenttype.domain.po.AgentTypePO;
import org.spring.springboot.app.set.agenttype.domain.vo.AgentTypeVO;
import org.spring.springboot.app.set.agenttype.service.IAgentTypeService;
import org.spring.springboot.app.sys.dict.domain.po.SysDictPO;
import org.spring.springboot.app.sys.dict.domain.vo.SysDictVO;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(description="代理商类型接口",tags = ApiIndex.AgentTypeController)
@RequestMapping(value = "/api/setAgentType")
@RestController
public class AgentTypeController {

    @Autowired
    private IAgentTypeService agentTypeService;

    @ApiOperation(value = "查询代理商类型列表-分页")
    @RequestMapping(value = "", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    public R<PageInfo<List<AgentTypePO>>> findPage(
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "20") int pageSize
    ) {
        R<PageInfo<List<AgentTypePO>>> result = new R<PageInfo<List<AgentTypePO>>>();
        List<AgentTypePO> _list = agentTypeService.findPage(pageNum, pageSize);
        PageInfo p = new PageInfo(_list);
        result.setData(p);
        return result;
    }

    @ApiOperation(value = "查询所有代理商类型")
    @RequestMapping(value = "/findAllAgentType", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    public R<List<AgentTypePO>> findAllAgentType( ) {
        R result = new R();
        List<AgentTypePO> _list = agentTypeService.selectAllAgentType();
        result.setData(_list);
        return result;
    }

    @ApiOperation(value = "保存代理商类型信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.POST)
    public R save(
            @ApiParam(value = "代理商类型对象", required = true) @RequestBody AgentTypeVO agentTypeVO
    ) {
        R result = new R();
        AgentTypeVO po = new AgentTypeVO();
        BeanUtils.copyProperties(agentTypeVO, po);
        int code = agentTypeService.save(po);
        if (code == 0) {
            result.setType(Type.OperError);
            result.setMessage("保存失败");
        }
        return result;
    }

    @ApiOperation(value = "修改代理商类型信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public R update(
            @ApiParam(value = "代理商类型对象", required = true) @RequestBody AgentTypePO agentTypePO
    ) {
        R result = new R();
        AgentTypePO po = new AgentTypePO();
        BeanUtils.copyProperties(agentTypePO, po);
        int code = agentTypeService.update(po);
        if (code == 0) {
            result.setType(Type.OperError);
            result.setMessage("修改失败");
        }
        return result;
    }

    @ApiOperation(value = "删除代理商类型信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public R delete(
            @ApiParam(value = "代理商类型id", required = true) @PathVariable("id") String id
    ) {
        R result = new R();
        int code = agentTypeService.delete(id);
        if (code == 0) {
            result.setType(Type.OperError);
            result.setMessage("删除失败");
        }
        return result;
    }

    @ApiOperation(value = "查看最大的排序号")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/findMaxSort", method = RequestMethod.GET)
    public R findMaxSort() {
        R result = new R();
        int maxSort = agentTypeService.findMaxSort();
        result.setData(maxSort);
        return result;
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @ApiOperation(value = "批量删除代理商类型信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/deletes", method = RequestMethod.DELETE)
    public R deletes(@ApiParam(value = "代理商分类ids(用,分割)", required = true) @RequestParam("ids") String ids) {
        R result = new R();
        int code = agentTypeService.deletes(ids);
        if (code == 0) {
            result.setCode(1);
            result.setMessage("刪除失败");
        }
        return result;
    }
}
